$(document).ready(function(){
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var modulo_f1;
//var haight = new google.maps.LatLng(19.416668, -99.116669);
var browserSupportFlag =  new Boolean();
var org;
if(navigator.geolocation)
{
 console.log("Tiene soporte para el API");
 navigator.geolocation.getCurrentPosition(manejoPosicion,manejoErrores);
}else{
console.log("El navegador no soporta el API");
}
initialize();
});
 var directionsService = new google.maps.DirectionsService();  
function initialize() {
  modulo_f1 = new google.maps.LatLng(19.33135, -99.18448);
  directionsDisplay = new google.maps.DirectionsRenderer();
  var mapOptions = {
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  directionsDisplay.setMap(map);
}

function manejoPosicion(position) {
     org = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

    /* API Google Maps */
      var infowindow = new google.maps.InfoWindow({
        map: map,
        position: org,
        content: 'Tú ubicación'
      });

      infowindow=new google.maps.InfoWindow({
        map: map,
        position: modulo_f1,
        content: 'Módulo de información F1 en México DF'
      });

      map.setCenter(org);
      calcRoute();
    }


  function calcRoute() {
  var selectedMode ="DRIVING";
  var request = {
      origin: org,
      destination: modulo_f1,
      // Note that Javascript allows us to access the constant
      // using square brackets and a string value as its
      // "property."
      travelMode: google.maps.TravelMode[selectedMode]
   };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

   function manejoErrores(error) {
        /*
      Manejo de errores
    */
        switch (error.code) {
            case error.PERMISSION_DENIED:
          console.log("El acceso a la ubicación esta desactivado:"+error.PERMISSION_DENIED);
                break;
            case error.POSITION_UNAVAILABLE:
          console.log("La posición no esta disponible:"+error.POSITION_UNAVAILABLE);
                break;
            case error.TIMEOUT:
                console.log("La posición no puede ser determinada :"+error.TIMEOUT);
                break;
            default:
       console.log("Error:"+error);
                break;
        }
    }


